﻿using log4net;
using Microsoft.VisualBasic.Logging;
using Newtonsoft.Json;
using StoreProject.BLL;
using StoreProject.BLL.SettingBLL;
using StoreProject.DAL;
using StoreProject.Entity;
using StoreProject.Enum;
using StoreProject.Forms.SettingForms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static Dapper.SqlMapper;

namespace StoreProject.Forms
{
    public partial class UpdateOrderForm : Form
    {
        OrderEntity entity = null;
        OrderBLL _orderBll = new OrderBLL();
        WorkingProcedureBLL workingProcedureBLL = new WorkingProcedureBLL();
        CustomerBLL customerBLL = new CustomerBLL();
        LineTypeBLL lineTypeBLL = new LineTypeBLL();
        OperatorBLL operatorBLL = new OperatorBLL();
        ChangefulBLL changefulBLL = new ChangefulBLL();
        protected FormState _formState = FormState.INIT;

        ILog _log = LogManager.GetLogger("UpdateOrderForm");
        public UpdateOrderForm()
        {
            InitializeComponent();
        }
        public UpdateOrderForm(FormState state, OrderEntity entity)
        {
            _formState = state;
            this.entity = entity;
            InitializeComponent();
        }

        private void btnSaveOrder_Click(object sender, EventArgs e)
        {
            try
            {
                OrderEntity model = GetModel();
                _log.Info("开始修改制令单：");
                _log.Info(string.Format("修改的制令单的基础数据为：{0}", JsonConvert.SerializeObject(model)));
                bool res = false;
                if (model != null)
                    res = _orderBll.Update(model);
                if (res)
                {
                    var newchangeEntity = changefulBLL.GetList().ToList();
                    _log.Info(string.Format("修改后的批号为：{0}", newchangeEntity[0].BatchNo));
                    MessageBox.Show("保存成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Close();
                }
                else
                {
                    MessageBox.Show("保存失败", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch(Exception ex) 
            {
                _log.Info(ex.Message);
            }
            
        }

        private void UpdateOrderForm_Load(object sender, EventArgs e)
        {
            cmbProcessFlow.DisplayMember = "ProcessFlowName";
            cmbProcessFlow.ValueMember = "Id";
            cmbLineType.DisplayMember = "LineTypeName";
            cmbLineType.ValueMember = "LineTypeName";
            cmbCustomer.DisplayMember = "CustomerName";
            cmbCustomer.ValueMember = "CustomerName";
            cmbOperator.DisplayMember = "OperatorName";
            cmbOperator.ValueMember = "OperatorName";
            InitData();

        }
        private OrderEntity GetModel()
        {
            entity.UpdateTime = DateTime.Now;
            entity.Batch = this.txtBatch.Text;
            entity.FinishedProductName = this.txtFinishedProductName.Text;
            entity.PlanNums = Convert.ToInt32(this.numPlanNum.Value);
            entity.SemiFinishedProductName = this.txtSemiFinishedProductName.Text;
            entity.LineType = this.cmbLineType.Text;
            entity.Customer = this.cmbCustomer.Text;
            entity.RequestedDate = this.dateTimeRequestedDate.Value;
            entity.JuanNum = this.txtJuanNum.Text;
            entity.Operator = this.cmbOperator.Text;
            entity.DanZhong = this.txtDanZhong.Text;
            this.entity.DomesticAndExportSales = this.cmbDomesticAndExportSales.Text;
            var item = cmbProcessFlow.SelectedItem as ProcessFlow;
            if (item != null)
                entity.ProcessFlowId = item.Id;
            return entity;
        }

        private void InitData()
        {
            //工序数据源配置
            List<ProcessFlow> list = new List<ProcessFlow>();
            var groupList = workingProcedureBLL.GetListEx().GroupBy(x => x.ProcessFlowId);
            foreach (var group in groupList)
            {
                ProcessFlow processFlow = new ProcessFlow();
                processFlow.Id = group.Key;
                string temp = "";
                foreach (var item in group.ToList().OrderBy(x => x.IndexNum))
                {
                    temp += (item.IndexName + "+");
                }
                if (!string.IsNullOrEmpty(temp))
                    temp = temp.Remove(temp.Length - 1, 1);
                processFlow.ProcessFlowName = temp;
                list.Add(processFlow);

            }
            cmbProcessFlow.DataSource = list;

            //客户数据源配置
            var customer = customerBLL.GetList().ToList();
            cmbCustomer.DataSource = customer;

            //线型数据源配置
            var linetype = lineTypeBLL.GetList().ToList();
            cmbLineType.DataSource = linetype;

            //操作员数据源配置
            var operatorEx = operatorBLL.GetList().ToList();
            cmbOperator.DataSource = operatorEx;

            if (this.entity != null)
            {
                OrderEntity orderEntity = this.entity;
                this.cmbDomesticAndExportSales.Text = this.entity.DomesticAndExportSales;
                this.cmbOrderId.Text = this.entity.OrderId;
                this.txtBatch.Text = orderEntity.Batch;
                _log.Info(string.Format("修改前的批号为：{0}", orderEntity.Batch));
                this.txtFinishedProductName.Text = orderEntity.FinishedProductName;
                this.numPlanNum.Value = orderEntity.PlanNums;
                this.txtSemiFinishedProductName.Text = orderEntity.SemiFinishedProductName;
                this.cmbLineType.Text = orderEntity.LineType;
                this.cmbCustomer.Text = orderEntity.Customer;
                this.dateTimeRequestedDate.Value = orderEntity.RequestedDate;
                this.txtJuanNum.Text = orderEntity.JuanNum;
                this.cmbOperator.Text = orderEntity.Operator;
                this.txtDanZhong.Text = orderEntity.DanZhong;
                var temp = workingProcedureBLL.GetListByStatus(orderEntity.ProcessFlowId).ToList()[0];
                cmbProcessFlow.SelectedValue = temp.ProcessFlowId;
            }
        }
    }
}
